package com.mes.web.controller.tdd;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.mes.entity.cb.Materiel;
import com.mes.entity.cb.Order;
import com.mes.entity.cb.OrderDetailed;
import com.mes.entity.lc.MaterialComeku;
import com.mes.entity.tdd.Supplier;
import com.mes.entity.tdd.tj.Tpage;
import com.mes.service.hy.IMaterService;
import com.mes.service.jy.TempproductServise;
import com.mes.service.tdd.OthersService;
import com.mes.utils.DateJsonValueProcessor;
import com.mes.utils.ResponseUtil;
/**
 * 
 *@ClassName: OthersController 
 *@Description: 模块所需其他的方法
 *@Author tdd
 *@Date 2018-6-5 上午11:36:35 
 *@version 1.0
 */
@Controller
@RequestMapping("/others")
public class OthersController {
	@Resource
	public OthersService othersService;
	@Resource
	private TempproductServise tempproductServise;
	/**
	 * 采购订单分页查询
	 * @param pageBean 分页+条件
	 * @param limit 每页条数
	 * @param offset 条数
	 * @param response response对象
	 * @throws Exception 异常
	 */
	@RequestMapping("/oderlist")
	public void oderlist(Tpage pageBean,@RequestParam(value="limit",required=false)String limit,@RequestParam(value="offset",required=false)String offset,HttpServletResponse response) throws Exception {
		//页数
		pageBean.setPage(Integer.parseInt(offset)/Integer.parseInt(limit));
		//每页条数
		pageBean.setPageSize(Integer.parseInt(limit));
		List<Order> listOder = othersService.listOder(pageBean);
		int total=othersService.totalOder(pageBean);
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(listOder, jsonConfig);
		jsonObject.put("total", total);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	/**
	 * 根据订单查详情分页
	 * @param pageBean 分页+条件
	 * @param limit 每页条数
	 * @param offset 条数
	 * @param response response对象
	 * @throws Exception 异常
	 */
	@RequestMapping("/oderMlist")
	public void oderMlist(Tpage pageBean,@RequestParam(value="limit",required=false)String limit,@RequestParam(value="offset",required=false)String offset,HttpServletResponse response) throws Exception {
		
		//页数
		pageBean.setPage(Integer.parseInt(offset)/Integer.parseInt(limit));
		//每页条数
		pageBean.setPageSize(Integer.parseInt(limit));
		List<OrderDetailed> listOderM = othersService.listOderM(pageBean);
		int total=othersService.totalOderM(pageBean);
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(listOderM, jsonConfig);
		jsonObject.put("total", total);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	/**
	 * 根据订单ids查详情
	 * @param id 订单id集合
	 * @param response response对象
	 * @throws Exception 异常
	 */
	@RequestMapping("/allOderMByids")
	public void allOderMByids(String[] id,HttpServletResponse response) throws Exception {
		
		List<OrderDetailed> listOderM = new ArrayList<OrderDetailed>();
		String ids = null;
		for (int i = 0; i < id.length; i++) {
			if (i == 0) {
				ids = id[i];
			}else {
				ids = ids+","+id[i];
			}
		}
		listOderM = othersService.allOderMByids(ids);
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(listOderM, jsonConfig);
		jsonObject.put("total", null);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	/**
	 * 查询订单
	 * @param supplierid 供应商id
	 * @param remark17 是否完全到货状态    未 0,已完全到货1
	 * @param response response对象
	 * @throws Exception 异常
	 */
	@RequestMapping("/allOrder")
	public void allOrder(Long id,Long supplierid,String remark17, HttpServletResponse response) throws Exception {
		Gson gson = new Gson();
		Tpage pageBean = new Tpage();
		//审批状态
		pageBean.setStates("2");
		if (null != supplierid) {
			pageBean.setSupplierid(supplierid);
		}
		if(null != remark17){
			pageBean.setRemark17(remark17);
		}
		if(null != id){
			pageBean.setId(id);
		}
		List<Order> listOder = othersService.allOder(pageBean);
		ResponseUtil.write(response, gson.toJson(listOder));
	}
	
	
	/**
	 * 采购订单详细分页
	 * @param pageBean 条件+分页
	 * @param limit 每页条数
	 * @param offset 条数
	 * @param response response对象
	 * @throws Exception 异常
	 */
	@RequestMapping("/purpayList")
	public void purpayList(Tpage pageBean,@RequestParam(value="limit",required=false)String limit,@RequestParam(value="offset",required=false)String offset,HttpServletResponse response) throws Exception {
		//页数
		pageBean.setPage(Integer.parseInt(offset)/Integer.parseInt(limit));
		//每页条数
		pageBean.setPageSize(Integer.parseInt(limit));
		List<OrderDetailed> listOderM = othersService.listOderM(pageBean);
		int total=othersService.totalOderM(pageBean);
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(listOderM, jsonConfig);
		jsonObject.put("total", total);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	/**
	 * 采购对账
	 * @param pageBean 条件+分页
	 * @param limit 每页条数
	 * @param offset 条数
	 * @param response response对象
	 * @throws Exception 异常
	 */
	@RequestMapping("/reconciliationList")
	public void reconciliationList(Tpage pageBean,@RequestParam(value="limit",required=false)String limit,@RequestParam(value="offset",required=false)String offset,HttpServletResponse response) throws Exception {
		//页数
		pageBean.setPage(Integer.parseInt(offset)/Integer.parseInt(limit)+1);
		//每页条数
		pageBean.setPageSize(Integer.parseInt(limit));
		List<Materiel> listOderM = othersService.listCgdz(pageBean);
		int total=othersService.totalCgdz(pageBean);
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(listOderM, jsonConfig);
		jsonObject.put("total", total);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	/**
	 * 入库详情
	 * @param pageBean  条件+分页
	 * @param limit 每页条数
	 * @param offset 条数
	 * @param response response对象
	 * @throws Exception 异常
	 */
	@RequestMapping("/rkList")
	public void rkList(Tpage pageBean,@RequestParam(value="limit",required=false)String limit,@RequestParam(value="offset",required=false)String offset,HttpServletResponse response) throws Exception {
		//页数
		pageBean.setPage(Integer.parseInt(offset)/Integer.parseInt(limit)+1);
		//每页条数
		pageBean.setPageSize(Integer.parseInt(limit));
		List<MaterialComeku> listOderM = othersService.listRK(pageBean);
		int total=othersService.totalRK(pageBean);
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(listOderM, jsonConfig);
		jsonObject.put("total", total);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	
	
	
	/**
	 * 采购应付
	 * @param pageBean 条件+分页
	 * @param limit 每页条数
	 * @param offset 条数
	 * @param response response对象
	 * @throws Exception 异常
	 */
	@RequestMapping("/purPayList")
	public void purPayList(Tpage pageBean,@RequestParam(value="limit",required=false)String limit,@RequestParam(value="offset",required=false)String offset,HttpServletResponse response) throws Exception {
		//页数
		pageBean.setPage(Integer.parseInt(offset)/Integer.parseInt(limit)+1);
		//每页条数
		pageBean.setPageSize(Integer.parseInt(limit));
		List<Materiel> listOderM = othersService.listYf(pageBean);
		int total=othersService.totalYf(pageBean);
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(listOderM, jsonConfig);
		jsonObject.put("total", total);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	/**
	 * 采购应付详情
	 * @param pageBean 条件+分页
	 * @param limit 每页条数
	 * @param offset 条数
	 * @param response response对象
	 * @throws Exception 异常
	 */
	@RequestMapping("/purList")
	public void purList(Tpage pageBean,@RequestParam(value="limit",required=false)String limit,@RequestParam(value="offset",required=false)String offset,HttpServletResponse response) throws Exception {
		//页数
		pageBean.setPage(Integer.parseInt(offset)/Integer.parseInt(limit)+1);
		//每页条数
		pageBean.setPageSize(Integer.parseInt(limit));
		List<Supplier> listOderM = othersService.listPyf(pageBean);
		int total=othersService.totalPyf(pageBean);
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(listOderM, jsonConfig);
		jsonObject.put("total", total);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	
	//注入领料Server
	@Resource IMaterService iMaterService;
	
	/**
	 * bom计算
	 * @param pNummArr 品编
	 * @param plannum 计划量
	 * @param id 排产单id
	 * @param response response对象
	 * @param request request对象
	 * @throws Exception 异常
	 */
	@RequestMapping("/bom")
	public void bom(String[] pNummArr,
			 String[] plannum, String[] id,
			HttpServletResponse response, HttpServletRequest request)
			throws Exception {
		//物料bom集合
		List<Materiel> list = new ArrayList<>();
		
		
		
		// 选择排产单进入
		if (pNummArr.length > 0) {
			for (int i = 0; i < pNummArr.length; i++) {
				
				
				//根据产品编号查询这个产品对应的物料集合
				List<Materiel> list2= tempproductServise.selectBomByPnum(pNummArr[i]);
				//遍历这个集合，物料数量*产品数量 
				
				for (Materiel materiel : list2) {
					//绑定关系中物料对应的数量
					Integer mnum = Integer.valueOf(materiel.getRemark9());
					
					//num1产品需求数量
					Integer mnum2 = mnum*Integer.valueOf(plannum[i]);
					
					//计算后给物料赋值的数量
					materiel.setRemartk1(mnum2+"");
					
					
					for (Materiel materiel2 : list) {
						if (materiel2.getId().equals(materiel.getId())) {
							System.out.println(mnum2+Integer.valueOf(materiel2.getRemartk1())+"");
							materiel.setRemartk1(mnum2+Integer.valueOf(materiel2.getRemartk1())+"");
							list.remove(materiel2);
							break;
						}
					}
					
					list.add(materiel);
					
				}
				
				
				
				/*
				// 根据产品去关联表查询所需的物料（拿出一个产品去查关联物料，结果是个集合）
				maList = iMaterService.selMa(pNummArr[i]);
				
				for (int j = 0; j < maList.size(); j++) {
				
					// 根据物料id去cb物料表中查询物料基础信息,因为未改动之前的接口，所以是集合，本应是一个类（某一个）
					mat = iMaterService.queryMaterial(maList.get(j).getMaterid());
					if (mat != null) {
						mat.setRemartk1(plannum[i]);
						list.add(mat);
					}
				}*/
			}
		}
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		jsonObject.put("total", null);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}

}
